package com.imooc.reader.controller;

import com.imooc.reader.entity.Evaluation;
import com.imooc.reader.entity.Member;
import com.imooc.reader.exception.BussinessException;
import com.imooc.reader.service.MemberService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.Map;

@Controller
public class MemberController {
    @Resource
    private MemberService memberService;

    @GetMapping("/register.html")
    public ModelAndView showRegister() {
        ModelAndView modelAndView = new ModelAndView("/register");
        return modelAndView;
    }

    @PostMapping("/registe")
    @ResponseBody
    public Map registe(String vc, String username, String password, String nickname, HttpServletRequest request, HttpServletResponse response) {

        String verifyCode = (String) request.getSession().getAttribute("kaptchaVerifyCode");
        Map map = new HashMap();
        if (vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)) {
            map.put("code", "VC01");
            map.put("msg", "验证码错误");

        } else {
            try {
                memberService.createMember(username, password, nickname);
                map.put("code", "0");
                map.put("msg", "success");

            } catch (BussinessException e) {
                e.printStackTrace();
                map.put("code", e.getCode());
                map.put("msg", e.getMsg());
            }

        }

        return map;
    }

    @GetMapping("/login.html")
    public ModelAndView showLogin() {
        ModelAndView modelAndView = new ModelAndView("/login");
        return modelAndView;
    }

    @PostMapping("/check_login")
    @ResponseBody
    public Map checkLogin(String username, String password, String vc, HttpSession session) {
        Map map = new HashMap();
        String verifyCode = (String) session.getAttribute("kaptchaVerifyCode");

        if (vc == null || verifyCode == null || !vc.equalsIgnoreCase(verifyCode)) {
            map.put("code", "VC01");
            map.put("msg", "验证码错误");

        } else {
            try {
                Member member = memberService.checkLogin(username, password);
                session.setAttribute("loginMember", member);

                map.put("code", "0");
                map.put("msg", "success");
            } catch (BussinessException e) {
                e.printStackTrace();
                map.put("code", e.getCode());
                map.put("msg", e.getMsg());
            }
        }

        return map;
    }

    @PostMapping("/update_read_state")
    @ResponseBody
    public Map updateReadState(Long memberId,Long bookId,Integer readState){
        Map map = new HashMap();
        try {
            memberService.updateMemberReadState(memberId,bookId,readState);
            map.put("code","0");
            map.put("msg","success");
        } catch (BussinessException e) {
            e.printStackTrace();
            map.put("code", e.getCode());
            map.put("msg", e.getMsg());
        }

        return map;
    }

    @PostMapping("/evaluate")
    @ResponseBody
    public Map evaluate(Long memberId,Long bookId,Integer score,String content){
        Map map = new HashMap();
        try {
            Evaluation evaluation = memberService.evaluate(memberId, bookId, score, content);
            map.put("code","0");
            map.put("msg","success");
            map.put("evaluation",evaluation);

        } catch (BussinessException e) {
            e.printStackTrace();
            map.put("code", e.getCode());
            map.put("msg", e.getMsg());

        }
        return map;
    }

    @PostMapping("/enjoy")
    @ResponseBody
    public Map  enjoy(Long evaluationId){
        Map map = new HashMap();
        try {
            Evaluation evaluation = memberService.enjoy(evaluationId);
            map.put("code","0");
            map.put("msg","success");
            map.put("evaluation",evaluation);

        } catch (BussinessException e) {
            e.printStackTrace();
            map.put("code", e.getCode());
            map.put("msg", e.getMsg());

        }
        return map;
    }

}
